#include "logindialog.h"
#include "ui_logindialog.h"
#include "QMessageBox.h"
#include "QTextCodec.h"
#include <QPushButton>

QString LoginDialog::NICKNAME = "";

LoginDialog::LoginDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LoginDialog)
{
    ui->setupUi(this);
    //textChanged()文本框输入内容改变
    connect(ui->et_username,SIGNAL(textChanged(QString)),this,SLOT(getUserInfo(QString)));

    tableFlag=false;

    database = QSqlDatabase::addDatabase("QSQLITE");
    database.setDatabaseName("database.db");

    //打开数据库
    if(!database.open())
    {
        qDebug()<<database.lastError();
        qFatal("failed to connect.") ;
    }
    else
    {
        qDebug()<<"open success";
        QSqlQuery sql_query;        //变量必须在成功打开数据库后定义才有效
        sql_query.prepare(select_table);
        if(!sql_query.exec())
        {
            qDebug()<<sql_query.lastError();
        }
        else
        {
            QString tableName;
            while(sql_query.next())
            {
                tableName = sql_query.value(0).toString();
                qDebug()<<tableName;
                if(tableName.compare("user"))
                {
                    tableFlag=false;
                    qDebug()<<"table is not exist";
                }
                else
                {
                    tableFlag=true;
                    qDebug()<<"table is exist";
                }
            }
        }
        // 创建数据表
        if(tableFlag==false)
        {
            sql_query.prepare(create_sql);
            if(!sql_query.exec())
            {
                qDebug()<<sql_query.lastError();
            }
            else
            {
                qDebug()<<"table created!";
            }
        }

        //database.close();
    }

    ui->qq->hide();    // 让文本编辑器隐藏，也可以使用setVisible(false)函数
    ui->wechat->hide();    // 让文本编辑器隐藏，也可以使用setVisible(false)函数
    this->setFixedSize(512,345); //设置窗体固定大小
    ui->pushButton->setStyleSheet("border-image: url(:/images/back.png)");

    QString path=":/images/face.png";
//    QString diff="user"+QString::number(usr_id);
//    path.replace("userx",diff);
    //qDebug()<<path;

    QImage img;
    img.load(path);
    QPixmap pic=QPixmap::fromImage(img.scaled(ui->userPic->width(),ui->userPic->height()));

    ui->userPic->setPixmap(pic);
}

LoginDialog::~LoginDialog()
{
    delete ui;
}

void LoginDialog::changeEvent(QEvent *e) {
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void LoginDialog::on_login_clicked()
{
    if(matchFlag==false)
    {
        //用户名错误
        QMessageBox::warning(this, tr("警告"), tr("用户不存在！"), QMessageBox::Yes);
        this->ui->et_username->clear();
        this->ui->et_pwd->clear();
        this->ui->et_username->setFocus();
    }
    else
    {
        if(usr_passwd!=ui->et_pwd->text())
        {
            //密码错误
            QMessageBox::warning(this, tr("警告"), tr("用户不存在！"), QMessageBox::Yes);
            this->ui->et_username->clear();
            this->ui->et_pwd->clear();
            this->ui->et_username->setFocus();
        }
        else
        {
            //用户名和密码均正确
//            ChatWindow cw(this);
//            this->hide();
//            cw.show();
//            cw.exec();
//            this->close();
            LoginDialog::NICKNAME = usr_name;
            accept();
        }
    }
}


void LoginDialog::on_cancel_clicked()
{
//    this->close();
    //QApplication b();
    Register r(this);
    this->hide();
    r.show();
//    transmitdb(database);
    r.exec();
    this->show();
}

void LoginDialog::on_pushButton_toggled(bool checked)
{
    this->setFixedSize(512,390); //设置窗体固定大小
    ui->qq->setVisible(checked); // 设置文本编辑器的显示和隐藏
    ui->wechat->setVisible(checked); // 设置文本编辑器的显示和隐藏
    if(checked) {
//        ui->pushButton->setText(tr("收起"));
        ui->pushButton->setStyleSheet("border-image: url(:/images/more_unfold.png)");
        this->setFixedSize(512,390); //设置窗体固定大小
    }
    else {
//        ui->pushButton->setText(tr("展开"));
        ui->pushButton->setStyleSheet("border-image: url(:/images/back.png)");
        this->setFixedSize(512,345); //设置窗体固定大小
    }
}

//chatid,passwd,name,email,history
void LoginDialog::getUserInfo(QString name)
{

    QSqlQuery sql_query;        //变量必须在成功打开数据库后定义才有效

    //查询部分数据(name)

    QString tempstring="select * from user where name='"+name+"'";
    qDebug()<<tempstring;
    if(!sql_query.exec(tempstring))
    {
        qDebug()<<sql_query.lastError();
        matchFlag=false;
    }
    else
    {
        while(sql_query.next())
        {
            usr_id = sql_query.value(0).toInt();
            usr_passwd = sql_query.value(1).toString();
            usr_name = sql_query.value(2).toString();
            usr_email = sql_query.value(3).toString();
            usr_history = sql_query.value(4).toInt();

            qDebug()<<QString("chatid=%1    passwd=%2     name=%3       email=%4    history=%5").arg(usr_id).arg(usr_passwd).arg(usr_name).arg(usr_email).arg(usr_history);
        }
        if(usr_name==name)  matchFlag=true;
        else                matchFlag=false;
    }

    qDebug()<<matchFlag;
    if(matchFlag==true)
    {
        QString path=":/images/facex.jpg";
        QString diff="face"+QString::number(usr_id);
        path.replace("facex",diff);
        qDebug()<<path;

        QImage img;
        img.load(path);
        QPixmap pic=QPixmap::fromImage(img.scaled(ui->userPic->width(),ui->userPic->height()));
        ui->userPic->setPixmap(pic);
    }
    else
    {

        QPixmap pic;
        ui->userPic->setPixmap(pic);
    }


}

